package edu.sdjzu.exam.controller;

import edu.sdjzu.exam.common.CommonResponse;
import edu.sdjzu.exam.dto.message.SystemMessage;
import edu.sdjzu.exam.service.message.SystemMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/message")
public class messageController {
    @Autowired
    SystemMessageService messageService;
    //通过小组群发消息
    @PostMapping("addMessageByaGroup")
    public CommonResponse<Void> addMessageByaGroup(@RequestParam("message") String message, @RequestParam("groupId") Long groupId) {
        messageService.addMessageByaGroup(message,groupId);
        return CommonResponse.success("发送成功");
    }


    // 新增消息
    @PostMapping("addMessage")
    public CommonResponse<Void> addMessage(@RequestBody SystemMessage message) {
        messageService.addMessage(message);
        return CommonResponse.success("发送成功");
    }

    // 标记已读
    @PutMapping("/{id}/read")
    public CommonResponse<Void> markAsRead(@RequestParam Long id) {
        messageService.markAsRead(id);
        return CommonResponse.success("发送成功");
    }

    // 标记未读
    @PutMapping("/{id}/unread")
    public CommonResponse markAsUnread(@RequestParam Long id) {
        messageService.markAsUnread(id);
        return CommonResponse.success("该消息设为未读");
    }

    // 查询已读消息
    @GetMapping("/{studentId}/read")
    public CommonResponse<List<SystemMessage>> getReadMessages(@RequestParam String studentId) {
        List<SystemMessage> readMessages = messageService.getReadMessages(studentId);
        return CommonResponse.success("查询成功",readMessages);
    }

    // 查询未读消息
    @GetMapping("/{studentId}/unread")
    public CommonResponse<List<SystemMessage>> getUnreadMessages(@RequestParam String studentId) {
        List<SystemMessage> unreadMessages = messageService.getUnreadMessages(studentId);
        return CommonResponse.success("查询成功",unreadMessages);
    }
}
